

 const { watch, computed }  = require('../../../utils/computed')
 const request  = require('../../../utils/request')

Component({

    behaviors: [],
    properties: {
        foodss: Array,
        minSpend: Number,
        serverPrice: Number,
        type: String,
        foods: Array,    //实际中的购物数据
    },
    data: {
        move: {},
        goodsCart:true,
        foodCart: '', //用作标志
        //foods: wx.getStorageSync('foods')
        shopid: wx.getStorageSync('shopId')
    }, // 私有数据，可用于模版渲染

    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () { },
    moved: function () { },
    detached: function () { },
    ready: function(){
        this.animation = wx.createAnimation({
            duration: 500,
            timingFunction: 'ease',
        });
        computed(this, {
            count: function() {
                console.log(this.data.foods)
                let count = 0;
                if(this.data.foods.length>0) {
                    this.data.foods.forEach(val => {
                        count += val.count
                    })
                }
                return count
            },
            allPrice: function() {
                let price = 0;
                this.data.foods.forEach(val => {
                    price += val.count * val.discount * val.price
                })
                return Number(price)
            },
            allspecPrice: function() {  //多规格总价格
                let price2 = 0;
                this.data.foods.forEach( res =>{
                    if(res.spec){
                        let price1 = 0;
                        res.spec.forEach( val => {
                            price1 += Number(val.spec_price)
                        })
                        price2 += price1 * res.count
                    }
                })
                return Number(price2)
            },
            canBuy: function(){
                let price = 0;
                let allPrice = 0;
                this.data.foods.forEach(val => {
                    price += val.count * val.discount * val.price 
                })
                allPrice = price.toFixed(2);
                if(!allPrice || allPrice == 0){
                    return "请选择商品"
                }else{
                    if(allPrice < this.data.minSpend){
                        return `还差${(this.data.minSpend-allPrice).toFixed(2)}元`
                    }else{
                        return '立即下单'
                    }
                } 
            }
        })
        watch(this,{
            foodss: function(){
                let shopid = wx.getStorageSync('shopId');
                let that = this;
                let dishes = []
                setTimeout(function() {
                    let foods = wx.getStorageSync('foods') || [];
                    foods.forEach( res => {
                        if(res.shop_id == shopid){
                            dishes.push(res)
                        }
                    })
                    that.setData({
                        foods:dishes
                    })
                    console.log(dishes)
                },0)
                console.log(this.data.foods)
            },
            foods:function() {
                console.log(this.data.foods)
            }
        })
    },

    methods: {
        buyShop() {
            let food = JSON.stringify(this.data.foods);
            console.log(this.data.type)
            if(wx.getStorageSync('sessionId')){
                if(!this.data.allPrice || this.data.allPrice == 0){
                    return false
                }else{
                    if(this.data.allPrice < this.data.minSpend){
                        return false
                    }else{
                        if(this.data.type == '预定'){
                            wx.navigateTo({
                                url: '../../../shop/shopOrder/shopOrder'
                            })
                        }else{
                            wx.navigateTo({
                                url: '../../index/shop/shopOrder/shopOrder'
                            })
                        }
                    } 
                }
            }else{
                if(this.data.type == '预定'){
                    wx.navigateTo({
                        url: '../../../../my/myLogin/myLogin'
                    })
                }else{
                    wx.navigateTo({
                        url: '../../my/myLogin/myLogin'
                    })
                }
            }  
            
        },
        seeCart() {
            if(this.data.foods.count != 0){
                this.setData({
                    goodsCart: !this.data.goodsCart
                })
                //console.log(this.data.foods)
            }
            
        },
        handleCart(e) {
            //console.log(e)
            this.setData({
                goodsCart: !this.data.goodsCart
            })
        },

        //清空购物车
        clearCart() {
            try {
                wx.removeStorageSync('foods')
              } catch (e) {
                // Do something when catch error
              }
            this.setData({
                foods: [],
                goodsCart: !this.data.goodsCart
            })
            this.triggerEvent('clearCart', true)
        },

        //购物车数量变化出发函数
        foodMess(e) {
            let food = e.detail.food;   //购物车数据改变
            let specFlag = true;
            let price1 = 0;
            console.log(e.detail.food);
            if(food.spec){
                food.spec.forEach( res => {
                    price1 += Number(res.spec_price)
                })
                food.specPrice = price1;
                this.data.foods.forEach( (res,index) => {
                  if(res.id == food.id){
                    res.spec.forEach( (val1,i) => {
                      food['spec'].forEach( (val2,j) => {
                        specFlag = true;
                        if( i==j){
                          if(val1.spec_name != val2.spec_name){
                            specFlag = false;
                            return;
                          }
                        }
                      })
                    })
                    if(specFlag){   //表示已经找到对应的多规格菜式
                        this.data.foods[index] = food
                    }
                  }
                })
            }else{
                this.data.foods.forEach((res,index) => {
                    if(res.id == food.id){
                        this.data.foods[index] = food
                    }
                })
            }
            // this.data.foods.forEach( (res,index) => {
            //   if(res.count == 0){
            //     this.data.foods.splice(index,1)
            //   }
            // })
            console.log(this.data.foods)
            // this.setData({
            //     foods: this.data.foods
            // })

            this.triggerEvent('foodCart', {'allFoods':e.detail,'foods':this.data.foods},{ bubbles: true })


            //购物车是否到达最低起送价格
            let count = 0, price = 0, allPrice = 0, canBuy= '';
            this.data.foods.forEach(val => {
                count += val.count;
                price += val.count * val.discount * val.price;
            })
            allPrice = price.toFixed(2);
            if(allPrice == 0){
                canBuy= "请选择商品"
            }else{
                if(allPrice < this.data.minSpend){
                    canBuy= `还差${(this.data.minSpend-allPrice).toFixed(2)}元`
                }else{
                    canBuy= '立即下单'
                }
            }
            this.setData({
                count,
                allPrice,
                canBuy
            })
        },

        //字符串转化为数据
        floor(val){
            return Math.floor(val*100)/100
        },

        //计算多规格价格
        specPrice(){
            console.log(11)
            return '1212'
            // let price =0;
            // this.data.foods.spec.forEach( res => {
            //     price += Number(res.spec_price)
            // })
            // return (this.floor(dish.discount) + this.floor(price)) * dish.num
        }
    },
    

})